require(['jquery', 'cookie', 'app', "lay"], function ($) {
    require(['vue'], function (Vue) {
        var dic = new Vue({
            el: ".vue-box"
            , data: {
                where: "",
                that: {
                    id: "",
                    name: "全部",
                    code: "",
                    permissionsRole: [{
                        id: "",
                        name: "",
                        code: ""
                    }]
                },
                thatRole: {
                    id: "",
                    name: "",
                    code: "",
                    permissionsCode: []
                },
                thatPermissons: {
                    id: "",
                    name: "",
                    code: ""
                }
            }
            , created: function () {
                this.loadPage();
            }
            , methods: {
                loadPage: function () {
                    load();
                    this.that.permissionsRole = playGet("oauthuser/PermissionsController/permissions").data;
                    var toolbar = [];
                    toolbar.push({
                        "clas": "layui-icon layui-icon-add-1",
                        "eve": "ADD",
                        "tit": "新增",
                        "fnc": function (data) {
                            dic.openAdd();
                        }
                    });
                    var tool = [];
                    tool.push({
                        "clas": "layui-btn-normal",
                        "eve": "VIEW",
                        "tit": "查看权限",
                        "fnc": function (data) {
                            dic.that = data.data;
                        }
                    });
                    tool.push({
                        "clas": "layui-btn-danger",
                        "eve": "PUT",
                        "tit": "加入权限",
                        "fnc": function (data) {
                            dic.thatRole = data.data;
                            var str = "";
                            for (var index in data.data.permissionsRole) {
                                str = data.data.permissionsRole[index].code + "," + str;
                            }
                            dic.thatRole['permissionsCode'] = str;
                            alrYN($("#addPermissions"), function () {
                                var jon = dic.thatRole;
                                jon['permissionsCode'] = jon['permissionsCode'] instanceof Array ? jon['permissionsCode'] : jon['permissionsCode'].split(",");
                                var ret = playPut("oauthuser/RoleController/role/" + dic.thatRole.id, "", jon);
                                alr(ret.msg);
                            }, function () {

                            }, function () {

                            }, "加入权限", 1, ["600px", "200px"]);
                        }
                    });
                    treeTab($("#authoritiesTable"), "/oauthuser/RoleController/find/all", [
                        {type: 'numbers'},
                        {title: '操作', toolbar: '#authoritiesTbBar', align: 'center', width: 280},
                        {field: 'name', title: '角色名称', minWidth: 150},
                        {field: 'code', title: '角色标识', minWidth: 150},
                    ], function (data) {
                        debugger;
                    }, this.where, toolbar, tool);
                },
                seachs: function () {
                    this.where = fromName($(".el-form"));
                    this.loadPage();
                },
                refer: function () {

                },
                openAdd: function () {
                    alrYN($("#addRole"), function () {
                        var jon = dic.thatRole;
                        delete jon['id'];
                        var ret = playPost("oauthuser/RoleController/role", "", JSON.stringify(jon));
                        alr(ret.msg);
                    }, function () {

                    }, function () {

                    }, "新增角色", 1, ["600px", "350px"]);
                },
                addPermissions: function () {
                    alrYN($("#addPermissionsAll"), function () {
                        var jon = {"permissionsVos": [dic.thatPermissons]};
                        var ret = playPost("oauthuser/PermissionsController/permissions", "", JSON.stringify(jon));
                        alr(ret.msg);
                    }, function () {

                    }, function () {

                    }, "新增权限", 1, ["600px", "350px"]);
                }
            }
        });
    })
});